package com.platform.user.service;

import com.platform.user.dao.*;
import com.platform.user.dto.ChatRoomEnterApplicationDto;
import com.platform.user.dto.ChatRoomPreviewDto;
import com.platform.user.dto.ItemNoticeDetailDto;
import com.platform.user.entity.*;
import com.platform.user.websocket.SessionPool;
import com.platform.utils.PrintMessageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ChatRoomService {

    @Resource
    private ChatRoomDao chatRoomDao;
    @Resource
    private ChatUserDao chatUserDao;
    @Resource
    private ChatRoomEnterApplicationDao chatRoomEnterApplicationDao;
    @Resource
    private UserDao userDao;
    @Resource
    private LostHelpNoticeDao lostHelpNoticeDao;
    @Resource
    private ItemClaimNoticeDao itemClaimNoticeDao;

    public void exitChatRoom(Map<String, Object> params) {
        Integer userId = Integer.parseInt(params.get("userId").toString());
        Integer roomId = Integer.parseInt(params.get("roomId").toString());
        chatUserDao.deleteByUserIdAndRoomId(userId, roomId);
        ChatRoom chatRoom = chatRoomDao.queryById(roomId);
        chatRoom.setUserCount(chatRoom.getUserCount() - 1);
        chatRoomDao.update(chatRoom);
//        SessionPool.roomToSessionListMap
    }


    public ItemNoticeDetailDto queryItemByRoomId(Map<String, Object> params) {
        ChatRoom chatRoom = chatRoomDao.queryById(Integer.parseInt(params.get("roomId").toString()));
        Integer itemSort = chatRoom.getItemSort();
        Integer itemId = chatRoom.getItemId();
        ItemNoticeDetailDto itemNoticeDetailDto = new ItemNoticeDetailDto();
        itemNoticeDetailDto.setItemSort(itemSort);
        if (itemSort == 0) {
            LostHelpNotice lostHelpNotice = lostHelpNoticeDao.queryById(itemId);
            itemNoticeDetailDto.setLostHelpNotice(lostHelpNotice);
        } else {
            ItemClaimNotice itemClaimNotice = itemClaimNoticeDao.queryById(itemId);
            itemNoticeDetailDto.setItemClaimNotice(itemClaimNotice);
        }
        return itemNoticeDetailDto;
    }

    public Map<String, Object> queryRoomMember(Map<String, Object> params) {
        int roomId = Integer.parseInt(params.get("roomId").toString());
        List<ChatUser> chatUserList = chatUserDao.queryListByRoomId(roomId);
        ChatRoom chatRoom = chatRoomDao.queryById(roomId);

        Map<String, Object> resMap = new HashMap<>();
        List<User> userList = new ArrayList<>();
        List<User> userList2 = new ArrayList<>();

        for(ChatUser x: chatUserList) {
            User user = userDao.queryById(x.getUserId());
            user.setFullName(null);
            user.setEmail(null);
            user.setPassword(null);
            if (Objects.equals(chatRoom.getUserId(), user.getId())) {
                userList2.add(user);
            } else {
                userList.add(user);
            }
        }

        userList2.addAll(userList);

        resMap.put("userList", userList2);
        resMap.put("roomCreatorId", chatRoom.getUserId());
        return resMap;
    }

    public void handleEnterChatRoomApplication(Map<String, Object> params) {
        int applicationStatus = Integer.parseInt(params.get("applicationStatus").toString());
        Integer applicationId = Integer.parseInt(params.get("applicationId").toString());

        if (applicationStatus == 1) {
            ChatUser chatUser = new ChatUser();
            ChatRoomEnterApplication chatRoomEnterApplication = chatRoomEnterApplicationDao.queryById(applicationId);
            chatUser.setUserId(chatRoomEnterApplication.getApplicantId());
            chatUser.setRoomId(chatRoomEnterApplication.getRoomId());
            chatUser.setEnterTime(new Date());
            chatUser.setItemId(chatRoomEnterApplication.getItemId());
            chatUser.setItemSort(chatRoomEnterApplication.getItemSort());
            chatUserDao.insert(chatUser);
            ChatRoom chatRoom = chatRoomDao.queryById(chatRoomEnterApplication.getRoomId());
            chatRoom.setUserCount(chatRoom.getUserCount() + 1);
            chatRoomDao.update(chatRoom);
        }
        ChatRoomEnterApplication chatRoomEnterApplication = new ChatRoomEnterApplication();
        chatRoomEnterApplication.setId(applicationId);
        chatRoomEnterApplication.setApplicationStatus(applicationStatus);
        chatRoomEnterApplication.setCreatedTime(null);
        chatRoomEnterApplicationDao.update(chatRoomEnterApplication);
    }

    public List<ChatRoomPreviewDto> queryChatRoomList(Map<String, Object> params) {
        Integer userId = Integer.parseInt(params.get("userId").toString());
        String roomName = params.get("roomName").toString();
        List<ChatUser> chatUserList = chatUserDao.queryListByUserId(userId);
        List<ChatRoomPreviewDto> chatRoomPreviewDtoList = new ArrayList<>();
        List<ChatRoomPreviewDto> chatRoomPreviewDtoList2 = new ArrayList<>();

        for (ChatUser x: chatUserList) {
            ChatRoomPreviewDto chatRoomPreviewDto = new ChatRoomPreviewDto();
            chatRoomPreviewDto.setRoomId(x.getRoomId());
            chatRoomPreviewDto.setItemSort(x.getItemSort());
            if (x.getItemSort() == 0) {
                LostHelpNotice lostHelpNotice = lostHelpNoticeDao.queryById(x.getItemId());
                chatRoomPreviewDto.setImageCover(lostHelpNotice.getImageCover());
            } else {
                ItemClaimNotice itemClaimNotice = itemClaimNoticeDao.queryById(x.getItemId());
                chatRoomPreviewDto.setImageCover(itemClaimNotice.getImageCover());
            }
            ChatRoom chatRoom = chatRoomDao.queryById(x.getRoomId());
            chatRoomPreviewDto.setRoomName(chatRoom.getRoomName());
            chatRoomPreviewDto.setUserCount(chatRoom.getUserCount());
            chatRoomPreviewDtoList.add(chatRoomPreviewDto);
        }

        if (!roomName.isEmpty()) {
            for(ChatRoomPreviewDto x: chatRoomPreviewDtoList) {
                if (x.getRoomName().contains(roomName)) {
                    chatRoomPreviewDtoList2.add(x);
                }
            }
            return chatRoomPreviewDtoList2;
        } else {
            return chatRoomPreviewDtoList;
        }
    }

    public List<ChatRoomEnterApplicationDto> queryChatRoomEnterApplication(Map<String, Object> params) {
        List<ChatRoomEnterApplication> chatRoomEnterApplicationList = chatRoomEnterApplicationDao.queryListByParams(params);
        List<ChatRoomEnterApplicationDto> chatRoomEnterApplicationDtoList = new ArrayList<>();
        for (ChatRoomEnterApplication x: chatRoomEnterApplicationList) {
            ChatRoomEnterApplicationDto chatRoomEnterApplicationDto = new ChatRoomEnterApplicationDto();
            chatRoomEnterApplicationDto.setChatRoomEnterApplication(x);
            User user = userDao.queryById(x.getApplicantId());
            user.setFullName(null);
            user.setEmail(null);
            user.setPassword(null);
            chatRoomEnterApplicationDto.setUser(user);
            chatRoomEnterApplicationDtoList.add(chatRoomEnterApplicationDto);
        }
        return chatRoomEnterApplicationDtoList;
    }

    public int queryUserIfInRoom(Map<String, Object> params) {
        ChatUser chatUser = chatUserDao.queryOneByParams(params);
        if (chatUser == null) {
            return -1;
        }
        return chatUser.getRoomId();
    }

    public void applyEnterChatRoom(Map<String, Object> params) {
        Integer itemId = Integer.parseInt(params.get("itemId").toString());
        Integer itemSort = Integer.parseInt(params.get("itemSort").toString());
        Integer applicantId = Integer.parseInt(params.get("applicantId").toString());
        Integer publisherId = Integer.parseInt(params.get("publisherId").toString());
        String applicationReason = params.get("applicationReason").toString();
        params.put("applicationStatus", 0);
        ChatRoomEnterApplication chatRoomEnterApplication = chatRoomEnterApplicationDao.queryOneByParams(params);
        if (chatRoomEnterApplication != null) {
            chatRoomEnterApplication.setApplicationReason(applicationReason);
            chatRoomEnterApplication.setCreatedTime(new Date());
            System.out.println(chatRoomEnterApplication.toString());
            chatRoomEnterApplicationDao.update(chatRoomEnterApplication);
            System.out.println("更新申请");
        } else {
            ChatRoomEnterApplication newChatRoomEnterApplication = new ChatRoomEnterApplication();
            newChatRoomEnterApplication.setItemId(itemId);
            newChatRoomEnterApplication.setItemSort(itemSort);
            newChatRoomEnterApplication.setApplicantId(applicantId);
            newChatRoomEnterApplication.setPublisherId(publisherId);
            newChatRoomEnterApplication.setCreatedTime(new Date());
            newChatRoomEnterApplication.setApplicationReason(applicationReason);
            newChatRoomEnterApplication.setApplicationStatus(0);
            ChatRoom chatRoom = chatRoomDao.queryByItemIdAndItemSort(itemId, itemSort);
            newChatRoomEnterApplication.setRoomId(chatRoom.getId());
            chatRoomEnterApplicationDao.insert(newChatRoomEnterApplication);
            System.out.println("还没有申请过");
        }
    }
}
